package com.yxkj.syh.app.huarong.activities.creat.bank.edit;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Payment;
import com.yxkj.syh.app.huarong.data_center.model.PaymentModel;

/**
 * 创建者：SYH
 * 时间：2020/5/21 16:51
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class EditBankCardsVM extends BaseViewModel<ActivityEvent> {
    private Payment payment;
    private String bankName = "网银";
    private long linkmanID;

    public EditBankCardsVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<Boolean> ofBankEnable = new ObservableField<>(Boolean.FALSE);
    public ObservableField<Boolean> ofAliEnable = new ObservableField<>(Boolean.TRUE);
    public ObservableField<Boolean> ofWechatEnable = new ObservableField<>(Boolean.TRUE);
    public ObservableField<Boolean> ofIsEdit = new ObservableField<>(Boolean.FALSE);
    public ObservableField<String> ofFormTitle1 = new ObservableField<>("持卡人姓名");
    public ObservableField<String> ofFormTitle2 = new ObservableField<>("银行卡号");
    public ObservableField<String> ofFormHint1 = new ObservableField<>("请输入持卡人姓名");
    public ObservableField<String> ofFormHint2 = new ObservableField<>("请输入银行卡号");
    public ObservableField<String> ofForm1 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm2.get(), ofForm3.get());
        }
    };
    public ObservableField<String> ofForm2 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm1.get(), ofForm3.get());
        }
    };
    public ObservableField<String> ofForm3 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm1.get(), ofForm2.get());
        }
    };
    public ObservableField<Boolean> ofDefault = new ObservableField<>(Boolean.FALSE);
    public ObservableField<Boolean> ofCommitEnable = new ObservableField<>(Boolean.FALSE);
    public MutableLiveData<Boolean> mldCommit = new MutableLiveData<>();

    private void commitEnable(String form1, String form2, String form3) {
        if (!TextUtils.isEmpty(form1) && !TextUtils.isEmpty(form2)) {
            if (ofBankEnable.get()) {

                if (TextUtils.isEmpty(form3)) {
                    if (!ofCommitEnable.get()) {
                        ofCommitEnable.set(true);
                    }
                }
            } else {
                if (!ofCommitEnable.get()) {
                    ofCommitEnable.set(true);
                }
            }
        } else {
            if (ofBankEnable.get()) {
                if (TextUtils.isEmpty(form3)) {
                    if (ofCommitEnable.get()) {
                        ofCommitEnable.set(false);
                    }
                }
            } else {
                if (ofCommitEnable.get()) {
                    ofCommitEnable.set(false);
                }
            }
        }
    }

    /**
     * 编辑收款方式 初始化
     *
     * @param payment
     */
    public void initPayment(Payment payment) {
        this.payment = payment;
        ofIsEdit.set(true);
        switch (payment.getBankName()) {
            case "网银":
                ofBankEnable.set(false);
                ofAliEnable.set(true);
                ofWechatEnable.set(true);
                ofFormTitle1.set("持卡人姓名");
                ofFormTitle2.set("银行卡号");
                ofFormHint1.set("请输入持卡人姓名");
                ofFormHint2.set("请输入银行卡号");
                ofForm3.set(payment.getOpeningBank());
                break;
            case "支付宝":
                ofAliEnable.set(false);
                ofBankEnable.set(true);
                ofWechatEnable.set(true);
                ofFormTitle1.set("真实姓名");
                ofFormTitle2.set("支付宝账户");
                ofFormHint1.set("请输入真实姓名");
                ofFormHint2.set("请输入支付宝账户");
                break;
            case "微信":
                ofWechatEnable.set(false);
                ofBankEnable.set(true);
                ofAliEnable.set(true);
                ofFormTitle1.set("真实姓名");
                ofFormTitle2.set("微信账户");
                ofFormHint1.set("请输入真实姓名");
                ofFormHint2.set("请输入微信账户");
                break;
        }
        ofForm1.set(payment.getAccountName());
        ofForm2.set(payment.getCardNumber());
        ofDefault.set(payment.getAuto() == 1);
    }

    public void setLinkmanID(long linkmanID) {
        this.linkmanID = linkmanID;
    }

    public ClickEvent bankClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (payment == null) {
                bankName = "网银";
                ofBankEnable.set(false);
                ofAliEnable.set(true);
                ofWechatEnable.set(true);
                ofFormTitle1.set("持卡人姓名");
                ofFormTitle2.set("银行卡号");
                ofFormHint1.set("请输入持卡人姓名");
                ofFormHint2.set("请输入银行卡号");
                ofForm1.set("");
                ofForm2.set("");
                ofDefault.set(false);
            }
        }
    };

    public ClickEvent aliClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (payment == null) {
                bankName = "支付宝";
                ofBankEnable.set(true);
                ofAliEnable.set(false);
                ofWechatEnable.set(true);
                ofFormTitle1.set("真实姓名");
                ofFormTitle2.set("支付宝账户");
                ofFormHint1.set("请输入真实姓名");
                ofFormHint2.set("请输入支付宝账号");
                ofForm1.set("");
                ofForm2.set("");
                ofDefault.set(false);
            }
        }
    };

    public ClickEvent wechatClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (payment == null) {
                bankName = "微信";
                ofBankEnable.set(true);
                ofAliEnable.set(true);
                ofWechatEnable.set(false);
                ofFormTitle1.set("真实姓名");
                ofFormTitle2.set("微信账户");
                ofFormHint1.set("请输入真实姓名");
                ofFormHint2.set("请输入微信账号");
                ofForm1.set("");
                ofForm2.set("");
                ofDefault.set(false);
            }
        }
    };

    public ClickEvent commitClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            Payment tempP;
            if (payment == null) {//添加
                tempP = new Payment();
                tempP.setBankName(bankName);
                tempP.setAccountName(ofForm1.get());
                tempP.setCardNumber(ofForm2.get());
                tempP.setOpeningBank(ofForm3.get());
                tempP.setAuto(ofDefault.get() ? 1 : 0);
                tempP.setSupplierId(linkmanID+"");
            } else {//编辑
                tempP = payment;
                tempP.setAccountName(ofForm1.get());
                tempP.setCardNumber(ofForm2.get());
                tempP.setOpeningBank(ofForm3.get());
                tempP.setAuto(ofDefault.get() ? 1 : 0);
            }

            showLoading("editPayment");
            PaymentModel.getPaymentModel().editPayment(tempP.getRequest(), mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
                @Override
                public void onSuccess(BaseResponse response) {
                    hideLoading("editPayment");
                    Tooast.success("提交成功");
                    mldCommit.setValue(true);
                }

                @Override
                public void onFailed(int code, String msg) {
                    hideLoading("editPayment");
                    Tooast.warning(msg);
                }
            });
        }
    };

}
